package top.hcode.hoj.dao.course;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.nutz.lang.Lang;
import org.sagacity.sqltoy.model.Page;
import org.springframework.stereotype.Service;
import top.hcode.hoj.dao.BaseDao;
import top.hcode.hoj.mapper.StuffMapper;
import top.hcode.hoj.pojo.dto.QueryStuffDTO;
import top.hcode.hoj.pojo.entity.course.Stuff;
import top.hcode.hoj.pojo.vo.StuffTypeCountVO;
import top.hcode.hoj.pojo.vo.StuffVO;
import top.hcode.hoj.utils.PageUtil;

import java.util.List;
import java.util.Map;

/**
 * @author orangej
 * @since 2024/12/18
 */
@Service
public class StuffDao extends BaseDao<StuffMapper, Stuff> {

    public IPage<StuffVO> adminQuery(QueryStuffDTO dto) {
        Map<String, Object> params = Lang.obj2map(dto);
        Page<StuffVO> pager = lazyDao.findPageBySql(new Page<>(dto.getLimit(), dto.getCurrentPage()),
                "adminQueryStuff", params, StuffVO.class);

        List<StuffVO> voList = pager.getRows();
        linkLoader.loadTags(voList);

        return PageUtil.toMybatis(pager);
    }

    public List<StuffTypeCountVO> queryTypeCount(QueryStuffDTO dto) {
        Map<String, Object> params = Lang.obj2map(dto);
        return lazyDao.selectList(StuffTypeCountVO.class, "adminStatStuffCountByType", params);
    }

    public void removeChildrenOf(List<String> idList) {
        if (idList.isEmpty()) return;
        baseMapper.delete(new UpdateWrapper<Stuff>().in("parent_id", idList));
    }
}
